/**
 * @param {character[][]} board
 * @return {number}
 */
var numRookCaptures = function(board) {
  let count = 0, // 碰到的卒的个数
      x = 0, // 车的横坐标 
      y = 0 // 车的纵坐标
  let direction = [ [-1, 0], [0, 1], [1, 0], [0, -1] ] // 移动的四个方向

  // 初始化车的坐标位置
  for (let i = 0; i < board.length; i++) {
    for (let j = 0; j < board[0].length; j++) {
      if (board[i][j] === 'R') {
        x = i
        y = j
        // console.log(x, y, 'xy')
      }
    }
  }

  // 开始向四个方向广度遍历
  for (let i = 0; i < 4; i++) { // 控制四个方向
    for (let j = 0; j < 8; j++) { // 控制每个方向移动次数
      let rx = x + direction[i][0] * j // 横坐标移动
      let ry = y + direction[i][1] * j // 纵坐标移动
      if (rx < 0 || rx >= 8 || ry < 0 || ry >= 8 || board[rx][ry] === 'B') break // 剪枝
      if (board[rx][ry] === 'p') { // 找到卒
        count++
        break
      }
    }
  }
  return count
};

console.log(numRookCaptures([[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]))
console.log(numRookCaptures([[".",".",".",".",".",".",".","."],[".","p","p","p","p","p",".","."],[".","p","p","B","p","p",".","."],[".","p","B","R","B","p",".","."],[".","p","p","B","p","p",".","."],[".","p","p","p","p","p",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]))
console.log(numRookCaptures([[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]]))